package com.warehouse.module.system.service.appuser;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.warehouse.module.system.controller.admin.appuser.vo.*;
import com.warehouse.module.system.dal.dataobject.appuser.AppUserDO;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.util.object.BeanUtils;

import com.warehouse.module.system.dal.mysql.appuser.AppUserMapper;

import static com.warehouse.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.warehouse.module.system.enums.ErrorCodeConstants.*;

/**
 * APP用户 Service 实现类
 *
 * @author Eric
 */
@Service
@Validated
public class AppUserServiceImpl implements AppUserService {

    @Resource
    private AppUserMapper appUserMapper;

    @Override
    public Long createAppUser(AppUserSaveReqVO createReqVO) {
        // 插入
        AppUserDO appUser = BeanUtils.toBean(createReqVO, AppUserDO.class);
        appUserMapper.insert(appUser);
        // 返回
        return appUser.getId();
    }

    @Override
    public void updateAppUser(AppUserSaveReqVO updateReqVO) {
        // 校验存在
        validateAppUserExists(updateReqVO.getId());
        // 更新
        AppUserDO updateObj = BeanUtils.toBean(updateReqVO, AppUserDO.class);
        appUserMapper.updateById(updateObj);
    }

    public int updateUserPhone(String appId, String openid, String phone){
        AppUserDO v1 = getAppUserByAppIdPhone(appId, phone);
        if(v1 != null){
            throw exception(APP_USER_PHONE_EXISTS);
        }
        LambdaUpdateWrapper<AppUserDO> updateWrapper = Wrappers.lambdaUpdate(AppUserDO.class)
                .eq(AppUserDO::getOpenid, openid)
                .eq(AppUserDO::getAppId, appId)
                .set(AppUserDO::getPhoneNumber, phone);
        return appUserMapper.update(updateWrapper);
    }

    public int updateUserOpenId(String appId, String openid, String phone){
        AppUserDO v1 = getAppUserByAppIdOpenId(appId, openid);
        if(v1 != null){
            throw exception(APP_USER_OPENID_EXISTS);
        }
        LambdaUpdateWrapper<AppUserDO> updateWrapper = Wrappers.lambdaUpdate(AppUserDO.class)
                .set(AppUserDO::getOpenid, openid)
                .eq(AppUserDO::getAppId, appId)
                .eq(AppUserDO::getPhoneNumber, phone);
        return appUserMapper.update(updateWrapper);
    }

    @Override
    public void deleteAppUser(Long id) {
        // 校验存在
        validateAppUserExists(id);
        // 删除
        appUserMapper.deleteById(id);
    }

    private void validateAppUserExists(Long id) {
        if (appUserMapper.selectById(id) == null) {
            throw exception(APP_USER_NOT_EXISTS);
        }
    }

    @Override
    public AppUserDO getAppUser(Long id) {
        return appUserMapper.selectById(id);
    }

    @Override
    public PageResult<AppUserDO> getAppUserPage(AppUserPageReqVO pageReqVO) {
        return appUserMapper.selectPage(pageReqVO);
    }

    @Override
    public AppUserDO getAppUserByAppIdOpenId(String appId, String openid){
        LambdaQueryWrapper<AppUserDO> query = Wrappers.lambdaQuery(AppUserDO.class)
                .eq(AppUserDO::getOpenid, openid)
                .eq(AppUserDO::getAppId, appId);
        return appUserMapper.selectOne(query);
    }

    @Override
    public AppUserDO getAppUserByAppIdPhone(String appId, String phone){
        LambdaQueryWrapper<AppUserDO> query = Wrappers.lambdaQuery(AppUserDO.class)
                .eq(AppUserDO::getPhoneNumber, phone)
                .eq(AppUserDO::getAppId, appId);
        return appUserMapper.selectOne(query);
    }

    public List<AppUserDO> listCompanyUsers(Long companyId){
        LambdaQueryWrapper<AppUserDO> query = Wrappers.lambdaQuery(AppUserDO.class)
                .eq(AppUserDO::getCompanyId, companyId);
        return appUserMapper.selectList(query);
    }

}